package sf.dsl.example;

import sf.database.dialect.DBDialect;
import sf.database.jdbc.sql.SQLContext;
import sf.database.jdbc.sql.SQLParameter;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 查询的 table，
 * 实例1：用于构建 select (select ...) as a from b 中的第二个 select
 * 实例2：用于构建 left join (select ...) 中的 select
 */
public class WrapperSimpleField extends SimpleField {

    private Example query;

    public WrapperSimpleField(Example query) {
        super();
        this.query = query;
    }

    public Example getQuery() {
        return query;
    }

    public void setQuery(Example query) {
        this.query = query;
    }

    @Override
    public void toConditionSql(StringBuilder sb, List<SQLParameter> list, AtomicBoolean useDefaultTableNameAlias, boolean useTablePrefix, DBDialect dialect) {
        query.dialect = dialect;
        SQLContext sqlContext = ExampleSQL.getSelectSQLContext(query, null, SelectOpt.all, true);
        String sql = sqlContext.getSql();
        if (list != null) {
            list.addAll(sqlContext.getParas());
        }
        sb.append('(').append(sql).append(')');
    }
}
